package controller;

import enums.SysInfoEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.SysM;
import pojo.User;
import service.SysMService;
import utils.ResultUtils;
import utils.SysTyepUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * @author jesse
 * @Date 2019/5/24 16:00
 * @Version 1.0
 */
@Controller
@RequestMapping("/SysM")
@Slf4j
public class SysMController {

    @Autowired
    private SysMService sysMService;

    @RequestMapping(value = "/get")
    @ResponseBody
    public SysM getOneSysM(@RequestParam(value = "username") String username){
        return sysMService.getOne(username);
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public String addOne(@RequestBody SysM sysM){
        System.out.println(sysM.toString());
        Integer result = sysMService.insert(sysM);
        return ResultUtils.result(result);
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public String update(@RequestBody SysM sysM){
        System.out.println(sysM.toString());
        Integer result = sysMService.update(sysM);
        return ResultUtils.result(result);
    }

    @RequestMapping(value = "/valid")
    @ResponseBody
    public String valid(@Valid User user, HttpSession session){
        SysM sysM;
        log.info("valid:user="+user);
        try {
            sysM = sysMService.getOne(user.getUsername());
        }catch (Exception e){
            e.printStackTrace();
            return SysInfoEnum.ERROR.getMessage();
        }
        log.info("valid:sysM="+sysM);
        User user1 = (User)session.getAttribute("USER_SESSION");
        log.info("valid:重复验证user="+user1);
        /**
         * 验证重复登录
         */
        if(user1!=null){
            if(sysM.getUsername().equals(user1.getUsername())){
                session.invalidate();
                return SysInfoEnum.REPEAT_LOGIN.getMessage();
            }
        }
        /**
         * 验证是否登录
         */
        if(sysM.getPassword().equals(user.getPassword())){
            session.setAttribute("USER_SESSION",user);

            if(sysM.getImg() != null) {
                /**windows用“\\\\” mac用“/” web.xml 修改 <location>D:\img\</location> 路径，自选*/
                String[] sourceStrArray = sysM.getImg().split(SysTyepUtils.getSysType());
                String path = "";
                for (int i = sourceStrArray.length - 3; i < sourceStrArray.length; i++) {
                    /**windows和mac下不用修改*/
                    path += ("/" + sourceStrArray[i]);
                }
                session.setAttribute("img",path);

            }

            return SysInfoEnum.SUCCESS.getMessage();
        }
        return SysInfoEnum.ERROR.getMessage();
    }

    @RequestMapping(value = "/getAll")
    @ResponseBody
    public List<SysM> getAll(){
        return sysMService.getAll();
    }

    @RequestMapping(value = "/admin",method = RequestMethod.GET)
    public String admins(HttpSession session){
        User user = (User) session.getAttribute("USER_SESSION");
        session.setAttribute("username",user.getUsername());
        return "/WEB-INF/view/administrators/admins.jsp";
    }

    @RequestMapping(value = "/signin")
    public String signin(HttpSession session){
        User user1 = (User)session.getAttribute("USER_SESSION");
        if(user1 == null){
            return "/WEB-INF/view/signin/signin.html";
        }else{
            return "redirect:/demo/SysM/admin";
        }
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("USER_SESSION");
        User user1 = (User)session.getAttribute("USER_SESSION");
        log.info("logout:user1="+user1);
        return "redirect:/demo/SysM/signin";
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete(String id){
        Integer result = sysMService.delete(id);
        return ResultUtils.result(result);
    }


    @RequestMapping(value = "/toAdd")
    public String toAdd(){
        return "/WEB-INF/view/administrators/add.html";
    }

    @RequestMapping(value = "/toEdit")
    public String toEdit(String username, HttpServletRequest request){

        request.setAttribute("username",username);

        return "/WEB-INF/view/administrators/edit.jsp";
    }

    @RequestMapping(value = "/getBySysMArray",method = RequestMethod.POST)
    @ResponseBody
    public List<SysM> getByArray(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                    @RequestParam(value = "size",defaultValue = "4") Integer size){
        List<SysM> sysMS =  sysMService.getSysByArray(currentPage, size);
        System.out.println(sysMS);
//        map.put("number",userFeedbackDTOPage.getTotalElements());
        return sysMS;
    }

    @RequestMapping(value = "/getNumber",method = RequestMethod.POST)
    @ResponseBody
    public Integer getNumber(){
        List<SysM> sysMS = sysMService.getAll();
        return sysMS.size();
    }
}
